import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator'
import { CreateSubPlanDto } from './dto/create-sub-plan.dto'
import { UpdateSubPlanDto } from './dto/update-sub-plan.dto'
import { SubPlanService } from './sub-plan.service'

export const permissions = definePermission('study:plan', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
  COPY: 'copy',
} as const)

@ApiTags('SubPlan - 子计划')
@ApiSecurityAuth()
@Controller('admin/study/plan/sub-plan')
export class SubPlanController {
  constructor(private readonly subPlanService: SubPlanService) {}

  @ApiOperation({ summary: '创建子计划' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @Perm(permissions.CREATE)
  @Post()
  create(@Body() createSubPlanDto: CreateSubPlanDto) {
    return this.subPlanService.create(createSubPlanDto)
  }

  @ApiOperation({ summary: '复制子计划' })
  @ApiResponse({ status: 201, description: '复制成功' })
  @Perm(permissions.COPY)
  @Post('copy')
  copy(@Body() createSubPlanDto: CreateSubPlanDto) {
    return this.subPlanService.copy(createSubPlanDto)
  }

  @ApiOperation({ summary: '获取子计划列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.LIST)
  @Get()
  findAll() {
    return this.subPlanService.findAll()
  }

  @ApiOperation({ summary: '获取子计划详情' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.READ)
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.subPlanService.findOne(+id)
  }

  @ApiOperation({ summary: '更新子计划' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Perm(permissions.UPDATE)
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateSubPlanDto: UpdateSubPlanDto) {
    return this.subPlanService.update(+id, updateSubPlanDto)
  }

  @ApiOperation({ summary: '删除子计划' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Perm(permissions.DELETE)
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.subPlanService.delete(+id)
  }
}
